<?php

namespace app\admin\controller\fastflow\flow;

use app\common\controller\Backend;

/**
 * 消息配置
 *
 * @icon fa fa-envelope-o
 */
class Message extends Backend
{

    /**
     * Message模型对象
     * @var \app\admin\model\fastflow\flow\Message
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\fastflow\flow\Message;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function test()
    {
        if ($this->request->isGet()) {
            $message_id = input('id');
            $receiver_scope = [
                ['value' => 1, 'name' => '人员'],
                ['value' => 2, 'name' => '分组'],
            ];
            $this->view->assign(['message_id' => $message_id, 'receiver_scope' => $receiver_scope]);
            return $this->view->fetch();
        } elseif ($this->request->isPost()) {
            $param = input('row/a');
            $message_id = $param['message_id'];
            $scope = $param['receiver_scope'];
            $receiver = $param['receiver'];
            if ($receiver == '') {
                return ['code' => 0, 'msg' => '接收人/分组不能为空'];
            }
            $message_row = $this->model->find($message_id);
            if (!$message_row) {
                return ['code' => 0, 'msg' => '参数错误'];
            }
            $receivers = [];
            foreach (explode(',', $receiver) as $receiverId) {
                $receivers[] = ['id' => $receiverId, 'name' => ''];
            }
            $message_params = [
                'flowName' => '测试流程',
                'flowDescription' => '测试流程',
                'scope' => $scope,
                'scopeName' => $scope == 1 ? '人员' : '分组',
                'workerIds' => '1',
                'workerNames' => '张三',
                'checkmode' => '联合审批',
                'stepName' => '测试步骤',
                'preStepName' => '前一测试步骤',
                'bill' => 'test',
                'billId' => '1',
                'billName' => '测试单据',
                'agency' => '否',
                'agentId' => '',
                'agentName' => '',
                'principalIds' => '',
                'principalName' => '',
                'receivers' => $receivers,
            ];

            $method_name = 'send' . ucfirst($message_row['key']);
            $customMessage = new CustomMessage();
            $method_exists = method_exists($customMessage, $method_name);
            if ($method_exists) {
                $config_arr = json_decode($message_row['config'], true);
                $configs = [];
                foreach ($config_arr as $config) {
                    $configs[$config['key']] = $config['value'];
                }
                $templet = $message_row['templet'];
                $message_body = $customMessage->parseTemplet($templet, $message_params);
                $receivers = $customMessage->enumReceiver($message_params['scope'], $message_params['receivers']);
                $result = call_user_func([$customMessage, $method_name], $receivers, $configs, $message_body);
                return $result;
            } else {
                return ['code' => 0, 'msg' => '未找到自定义方法'];
            }


        }
    }


    /*
     * 导入
     */
    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
